/*
 * Copyright 2012 Chris Uyehara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.chrisuyehara.vista.rpc.pool;

import java.io.IOException;

import com.chrisuyehara.vista.rpc.client.RPCClient;

import com.chrisuyehara.vista.rpc.models.SymbolTable;

import com.chrisuyehara.vista.rpc.procedures.Constants;
import com.chrisuyehara.vista.rpc.procedures.IRemoteProcedure;

import com.chrisuyehara.vista.rpc.procedures.XWB.DeserializeTable;
import com.chrisuyehara.vista.rpc.procedures.XWB.Disconnect;


/**
 * Class description
 *
 *
 * @version        v1.0, 2014-01-20
 * @author         Jim Horner    
 */
public class RPCConnection {

    /** Field description */
    private RPCConnectionPool connectionPool = null;

    /** Field description */
    private RPCClient rpcClient = null;

    /** Field description */
    private SymbolTable symbolTable = null;

    /**
     * Constructs ...
     *
     *
     * @param rpcClient
     * @param symbolTable
     */
    public RPCConnection(RPCClient rpcClient, SymbolTable symbolTable) {

        this.rpcClient = rpcClient;
        this.symbolTable = symbolTable;
    }

    /**
     * Constructs ...
     *
     *
     * @param rpcClient
     * @param symbolTable
     * @param connectionPool
     */
    public RPCConnection(RPCClient rpcClient, SymbolTable symbolTable,
                         RPCConnectionPool connectionPool) {

        this.rpcClient = rpcClient;
        this.symbolTable = symbolTable;
        this.connectionPool = connectionPool;
    }

    /**
     * Method description
     *
     *
     * @param remoteProcedure
     *
     * @throws IOException
     */
    protected void call(IRemoteProcedure remoteProcedure) throws IOException {

        if (null == remoteProcedure) {
            throw new NullPointerException("remoteProcedure cannot be null");
        }

        if (remoteProcedure instanceof Disconnect) {

            rpcClient.disconnect();
            rpcClient = null;
            symbolTable = null;
            connectionPool = null;

        } else {
            rpcClient.call(remoteProcedure);
        }
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public RPCConnectionPool getConnectionPool() {
        return connectionPool;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public RPCClient getRpcClient() {
        return rpcClient;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public SymbolTable getSymbolTable() {
        return symbolTable;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public boolean isConnectionPooled() {
        return null != connectionPool;
    }

    /**
     * Method description
     *
     */
    public void release() {

        if (false == isConnectionPooled()) {
            return;
        }

        try {

            DeserializeTable deserializeTable = new DeserializeTable(symbolTable);

            rpcClient.call(deserializeTable);

            rpcClient.context(Constants.CONTEXT_SIGNON);

        } catch (IOException e) {}

        connectionPool.release(this);
    }

    /**
     * Method description
     *
     *
     * @param connectionPool
     */
    public void setConnectionPool(RPCConnectionPool connectionPool) {
        this.connectionPool = connectionPool;
    }
}
